<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="c job xs f">

  <xsl:import href="../style/base.xsl" />
  
	<xsl:variable name="ref" select="'/localhost/system/db/connections'"/>

	<xsl:param name="con"/>
	
  <xsl:template match="/">
  	<xsl:choose>
  		<xsl:when test="$con">
  			<job:system mgr="/localhost/system/db/connections" cmd="close">
  				<job:param name="connection" value="{$con}"/>
  			</job:system>
  			<job:feed stream="response" base="job">
  				<html>
  					<body>
  						<script type="text/javascript">
  							location.replace(location.pathname);
  						</script>
  					</body>
  				</html>
  			</job:feed>
  		</xsl:when>
  		<xsl:otherwise>
  			<html>
  				<head>
  					<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  					<title>DB Connections</title>
  					<link href="../css/global.css" rel="stylesheet" type="text/css" />
  				</head>
  				<body>
  					<xsl:apply-templates select="doc($ref)/*"/>
  				</body>
  			</html>
  		</xsl:otherwise>
  	</xsl:choose>
  </xsl:template>

  <xsl:template match="connections">
  	<h2>DB Connections</h2>
  	<p>Currently open connections to the DB Servers:</p>
  	<table border="1" cellspacing="0" cellpadding="1" style="font-size: 80%;">
  		<thead>
  			<tr>
  				<th>URL</th>
  				<th>Parameters</th>
  				<th title="Reuse state">State</th>
  			</tr>
  		</thead>
  		<tbody>
  			<xsl:apply-templates select="*"/>
  		</tbody>
  	</table>
		<xsl:variable name="sinceLastClose"
			select="round(f:total-seconds(current-dateTime() - xs:dateTime(@last-close)))"/>
  	<p>In total <xsl:value-of select="if (@count) then @count else 'No'"/> connection(s) managed so far.<br/>
  		Last one closed at <xsl:value-of select="xs:dateTime(@last-close)"/>
  		(<xsl:value-of select="$sinceLastClose"/>s ago).<br/>
  		Close interval: <xsl:value-of select="@close-interval"/>s<br/>
  		Max idle time: <xsl:value-of select="@max-idle"/>s
  	</p>
  </xsl:template>

  <xsl:template match="connection">
    <tr>
      <td><xsl:value-of select="@url"/></td>
      <td>
        <xsl:for-each select="param">
        	<xsl:if test="position() > 1">, </xsl:if>
        	<xsl:value-of select="@name"/>
        	<xsl:text>: </xsl:text>
        	<xsl:value-of select="."/>
        </xsl:for-each>
      </td>
    	<td align="right">
    		<xsl:choose>
    			<xsl:when test="@last-closed">
    				<a onclick="location.replace('Connections.html?con={@id}')" style="cursor: pointer"
    					title="Click to close connection...">
    					idle for
     					<xsl:value-of
    						select="round(f:total-seconds(current-dateTime() - xs:dateTime(@last-closed)))"/>s
    				</a>
    			</xsl:when>
    			<xsl:otherwise>
    				<xsl:text>open</xsl:text>
    			</xsl:otherwise>
    		</xsl:choose>
    	</td>
    </tr>
  </xsl:template>
  
</xsl:stylesheet>
